package helperx

import (
	"fmt"
	"runtime"
	"strings"
	"time"

	"zhonghui/console/warehouse/structx"
)

// 常用时间格式

const TimeLayoutCommon = "2006-01-02 15:04:05"
const TimeLayoutYmd = "2006-01-02"
const TimeLayoutUYmd = "20060102"
const TimeLayoutYmdHis = "20060102150405"
const TimeLayoutISO8601 = "2006-01-02T15:04:05.000Z0700" // 8601时间格式
const TimeLayoutRFC3339 = time.RFC3339                   // 3339时间格式

func IsApp(platform string) bool {
	return IsIos(platform) || IsAndroid(platform)
}

func IsIos(platform string) bool {
	return strings.Contains(strings.ToLower(platform), "ios")
}

func IsAndroid(platform string) bool {
	return strings.Contains(strings.ToLower(platform), "android")
}

func IsMiniProgram(platform string) bool {
	return strings.Contains(strings.ToLower(platform), "mp")
}

// ParseNormalPager 解析传递来的页数和每页数量
func ParseNormalPager(p structx.NormalPagerReq) (int, int) {
	if p.Current == 0 {
		p.Current = 1
	}
	if p.PageSize == 0 {
		p.PageSize = 20
	}

	return p.Current, p.PageSize
}

// ParseGoodPager 解析传递来的页数和每页数量
func ParseGoodPager(p structx.GoodPagerReq) (uint64, int) {
	if p.Size == 0 {
		p.Size = 20
	}

	var realLastId int
	if p.LastId != "" {
		realLastId, _ = HashIdDecode(p.LastId)
	}

	return uint64(realLastId), p.Size
}

// MakeNormalPagerResp 生成翻页的返回数据
func MakeNormalPagerResp(total int64, size int) (r structx.NormalPagerResp) {
	r.Total = int(total)
	r.PageSize = size

	return
}

// MakeGoodPagerResp 生成翻页的返回数据
func MakeGoodPagerResp(total int64, realLastId uint64) (r structx.GoodPagerResp) {
	r.Total = int(total)

	if realLastId > 0 {
		h, _ := HashIdEncode(int(realLastId))
		r.LastId = h
	}

	return
}

// Callers 获取调用方信息
func Callers(skip int) string {
	p, f, l, ok := runtime.Caller(skip)
	fu := ""
	details := runtime.FuncForPC(p)
	if ok && details != nil {
		fu = details.Name()
	}

	return fmt.Sprintf("file: %s:%d func: %s", f, l, fu)
}
